package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.dao.IEmailNewsLetterRelaDao;
import com.magicode.cms.dao.INewsEnterpriseRelaDao;
import com.magicode.cms.entiy.base.EmailNewsLetterRela;
import com.magicode.cms.entiy.base.NewsEnterpriseRela;
import com.magicode.cms.service.IEmailNewsLetterRelaService;
import com.magicode.cms.service.INewsEnterpriseRelaService;

@Service("serviceEmailNewsLetterRela")
public class EmailNewsLetterRelaServiceImpl extends BaseServiceImpl<EmailNewsLetterRela,IEmailNewsLetterRelaDao>  implements IEmailNewsLetterRelaService{
	@Autowired
	@Qualifier("emailNewsLetterRelaDao")
	private IEmailNewsLetterRelaDao emailNewsLetterRelaDao;
	
	@Override
	public String getHql(Map<String, String> map) throws Exception {
		String hql ="from " +EmailNewsLetterRela.class.getName()+" "+entity_alias_name+" where 1 = 1 ";
		if(null!=map && !map.isEmpty()){
			if(map.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+map.get(field_id)+"'";
			}
			if(map.containsKey(field_title)){
				hql = hql+" and "+entity_alias_name+".title like '%"+map.get(field_title)+"%'";
			}
		}
		
		return hql;
	}


	
	@Override
	public List<Map<String,Object>> select(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere)+"order by "+entity_alias_name+".title";
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".enterprise.title as title ");
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	@Override
	public EmailNewsLetterRela getSourceByTitle(String name) throws Exception {
		try {
			Map<String,String> map = new HashMap<String,String> ();
			map.put("title", name);
			List<EmailNewsLetterRela> sources = this.getDao().queryDataList(this.getHql(map), 0, 0);
			return  sources.size()==1?sources.get(0) : null;
		} catch (Exception e) {
			//throw new Exception("getSourceByTitle error by email=["+name+"]"+e.getMessage());
			throw new Exception("getSourceByTitle error by title=["+name+"]"+e.getMessage());
		}
	}
	@Override
	public void deleteEmailNewsLetterRela(String userid) {
		// TODO Auto-generated method stub
		String hql = " from "+EmailNewsLetterRela.class.getName()+" t " +
				"  where  t.emailNewsLetter.id =:userid " ;
		Map map = new HashMap();
		map.put("userid", userid);
		List<NewsEnterpriseRela> vphs = this.emailNewsLetterRelaDao.getCommonDaoService().getListBySQLMap(hql,map);
		this.emailNewsLetterRelaDao.getCommonDaoService().deleteAll(vphs);
	}
	@Override
	public IEmailNewsLetterRelaDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return emailNewsLetterRelaDao;
	}
	
	@Override
	public void deleteEmailNewsLetterRelalist(String userid){
		emailNewsLetterRelaDao.deleteEmailNewsLetterRelalist(userid);
	}



	public IEmailNewsLetterRelaDao getEmailNewsLetterRelaDao() {
		return emailNewsLetterRelaDao;
	}



	public void setEmailNewsLetterRelaDao(
			IEmailNewsLetterRelaDao emailNewsLetterRelaDao) {
		this.emailNewsLetterRelaDao = emailNewsLetterRelaDao;
	}



	@Override
	public List<Map<String, Object>> getBeanInfo() {
		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		// TODO Auto-generated method stub
		return null;
	}


	
}
